<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\controller;
use \temmoku\db;
use \admin\home;
use \temmoku\lib\custom_model;
use \temmoku\lib\pages;
use admin\public_class\article;
class articles extends home
{
	private $home_modeldb;
	
	function __construct()
    {
    	parent :: __construct();
    	
    }
	
    public function add()
    {	
    	if('post'===$_POST['post']['step']){
			hook_listen('cms_post_begin');
    		(new Article)->check();
	    	$aid = (new article)->title($_POST['post']);
	    	//专题入库
	    	(new article)->post_article_special('post',$aid);
	    	//添加内容
	    	$arr_content=explode('##page##',$_POST['post']['content']);
			foreach($arr_content AS $value){
				$array['aid']=$aid;
				$array['content']=$value;
				(new article)->content($array);
				unset($array);
			}
			if($_POST['post']['mid']){
				$Model_Setting=C('home|home_modeldb.'.$_POST['post']['mid'])['field'];
				$module_post_data=(new Custom_Model)->module_post_data($Model_Setting,$_POST['_post']);
	    		(new article)->module($aid,$_POST['post']['mid'],$module_post_data);
			}
			hook_listen('cms_post_end');
    		$this->Success('发布成功', FromUrl);
    	}
    	if($_GET['cid']){
    		$cidDB=db::select('*')->from(jab."category")->where("cid='{$_GET['cid']}'")->row();
	    	$this->assign('mid', $cidDB['cmid']);
			if($cidDB['cmid']){
				$_ModelDB=C('home|home_modeldb.'.$cidDB['cmid'])['field'];
				if($_ModelDB){
					$Custom_Model_Template=	(new Custom_Model)->Custom_Model_Get($cidDB['cmid'],$_ModelDB);
					$this->assign('Custom_Model_Template', $Custom_Model_Template);
				}
			}
    	}

    	$special_list=(new Article)->get_all_special_list();
    	$this->assign('special_list', $special_list);
    	$moduleDB=db::select('*')->from(jab."module")->where(" close = 0")->query();
    	$allcidDB = (new article)-> allcidDB('0',jab.'category');
        $this->assign('allcidDB', $allcidDB);
        $this->assign('moduleDB', $moduleDB);
        $this->assign('cid', $_GET['cid']);
        $this->display(tpl('articles/add'));
    }
    
    public function index(){
    	!isset($_GET['page']) && $_GET['page']=1;
		!isset($_GET['cid']) && $_GET['cid']=0;
		$rows='20';
		$url="";
		if($_GET['cid']){
			$WHERE="cid = '$_GET[cid]'";
			$url.='cid_'.$_GET['cid'].'/';
		}else{
			$WHERE="1";
		}
		if($_GET['state']=='0' || $_GET['state']=='1'){
			$WHERE.=" AND state = '$_GET[state]'";
			$url.='state_'.$_GET['state'].'/';
		}
		if($_GET['is_recommend']=='1'){
			$WHERE.=" AND is_recommend = '$_GET[is_recommend]'";
			$url.='is_recommend_'.$_GET['is_recommend'].'/';
		}
		$data=db::select('*')->from(jab."article")->where($WHERE)->orderByDESC(['aid'])->setPaging($rows)->page($_GET['page'])->query();

		$Get_Page=(new Pages)->Pages(jab."article",$WHERE,"/admin/articles/index/{$url}",$rows,$_GET['page']);
    	$this->assign('data', $data);
    	$this->assign('Get_Page', $Get_Page);
    	$this->display(tpl('articles/index'));
    }
    
    public function edit(){
    	$edit=db::select('*')->from(jab."article")->where("aid= '{$_GET['aid']}' ")->row();
		if('post'===$_POST['post']['step']){
			hook_listen('cms_post_begin',$edit);
	    	(new Article)->check($edit);
			//专题入库
	    	(new Article)->post_article_special('edit',$_GET['aid']);
			//修改模型内容
	    	if($_POST['post']['mid']){
	    		$Model_Setting=C('home|home_modeldb.'.$_POST['post']['mid'])['field'];
	    		$edit_Custom_Model=db::select('*')->from(jab."content_".$edit['mid'])->where("aid= '{$edit['aid']}' ")->row();
				$module_post_data=(new Custom_Model)->module_post_data($Model_Setting,$_POST['_post'],$edit_Custom_Model);
	    		(new article)->module($_POST['post']['aid'],$_POST['post']['mid'],$module_post_data,'edit');
			}
			//修改主表内容
	    	(new article)->title($_POST['post'],'edit');
	    	//修改内容
	    	(new article)->content($_POST['post'],'edit');
			hook_listen('cms_post_end',$edit);
	    	$this->success('修改成功');
		}
		
		if($edit['mid']){
			$edit_Custom_Model=db::select('*')->from(jab."content_".$edit['mid'])->where("aid= '{$edit['aid']}' ")->row();
			$_ModelDB=C('home|home_modeldb.'.$edit['mid'])['field'];
			$Custom_Model_Template=(new Custom_Model)->Custom_Model_Get($edit['mid'],$_ModelDB,$edit_Custom_Model);
		}
		$cont_query=db::select('*')->from(jab."content")->where("aid= '{$edit['aid']}' ")->query();
		foreach($cont_query AS $V){
			$V['content']=str_replace  ( 'WWW_TEMMOKU_CN'  , WEBURL.'/'.C('upfiles_Catalog') ,  $V['content']);
			$cont_queryDB[]=$V;
		}
		
		$allcidDB = (new article)->allcidDB('0',jab.'category');
		$this->assign('allcidDB', $allcidDB);
		//专题
		$special_list=(new Article)->get_all_special_list();
    	$this->assign('special_list', $special_list);
		
		//获取已选择的专题
		$_selected_special_list=db::select('lid')->from(jab."special_article")->where(" aid = '{$_GET['aid']}'")->query();
		foreach($_selected_special_list AS $row){
			$selected_special_list[]=$row['lid'];
		}
		$this->assign('selected_special_list', $selected_special_list);
		
		$moduleDB=db::select('*')->from(jab."module")->where(" close = 0")->query();
		$edit['posttime']=date("Y-m-d H:i:s",$edit['posttime']);
		$this->assign('edit', $edit);
		$this->assign('cont', $cont);
		
		$this->assign('Custom_Model_Template', $Custom_Model_Template);
		$this->assign('mid', $edit['mid']);
		if($_GET['cid']){
			$edit['cid']=$_GET['cid'];
		}
		$this->assign('cid', $edit['cid']);
		$this->assign('cont_query', $cont_queryDB);
		$this->assign('moduleDB', $moduleDB);
	    $this->display(tpl('articles/add'));
    }
    
    public function del(){
		hook_listen('cms_articles_del',['aid'=>$_GET['aid']]);
    	(new Article)->del($_GET['aid']);
		$this->Success('删除成功');
    }

    public function ajax_Module(){
    	$_ModelDB=C('home|home_modeldb.'.$_GET['mid'])['field'];
    	if(!$_ModelDB){
    		echo '暂未创建字段'; 
    		exit;
    	}
    	$return=(new Custom_Model)->Custom_Model_Get($_GET['mid'],$_ModelDB);
    	echo $return;
    }
    //批量|单个审核或者推荐
    public function batch_edit(){
    	if($_POST['aid']){
    		if($_POST['state']=='1'||$_POST['state']=='0'){
    			$this->state($_POST['aid'],$_POST['state']);
    		}elseif($_POST['is_recommend']=='1' || $_POST['is_recommend'] =='0'){
    			$this->is_recommend($_POST['aid'],$_POST['is_recommend']);
    		}
    		echo json_encode(['Result'=>'1','content'=>'操作成功']);
    	}elseif($_POST['iddb'] && $_POST['step'] !='del'){
    		foreach($_POST['iddb'] AS $aid){
    			$aid=intval($aid);
    			if($_POST['state']=='1'||$_POST['state']=='0'){
	    			$this->state($aid,$_POST['state']);
	    		}elseif($_POST['is_recommend']=='1' || $_POST['is_recommend'] =='0'){
	    			$this->is_recommend($aid,$_POST['is_recommend']);
	    		}
    		}
    		$this->Success('操作成功');
    	}elseif($_POST['step']=='del'){
    		foreach($_POST['iddb'] AS $aid){
				hook_listen('cms_articles_del',['aid'=>$aid]);
    			(new Article)->del($aid);
    		}
    		$this->Success('操作成功');
    	}
    }
    
    private function state($aid,$state){
		hook_listen('cms_articles_state',['aid'=>$aid,'state'=>$state]);
    	db::update(jab."article")->cols(['state'=>$state,'state_time'=>NOWTIME])->where("aid='{$aid}'")->query();
    }
	
    private function is_recommend($aid,$is_recommend){
		hook_listen('cms_articles_recommend',['aid'=>$aid,'is_recommend'=>$is_recommend]);
    	if($is_recommend){
    		$reorder=",`reorder`=`reorder`+3600*7*24";
    	}else{
    		$reorder=",`reorder`=`reorder`-3600*7*24";
    	}
    	db::query("UPDATE ".jab."article SET `is_recommend` = '{$is_recommend}' {$reorder} WHERE aid='{$aid}'");
    }
}
?>
